﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;  
using System.Net;  
using System.Text;
using System.Xml; 

namespace Times
{
	public struct AddressStruct
	{
		public string Street;
		public string Zip;
		public string City;
		public string State;
		public string Latitude;
		public string Longitude;
	}

	public partial class AddressLookupForm : Form
	{
		public AddressLookupForm()
		{
			InitializeComponent();
		}

		public string Latitude = "";
		public string Longitude = "";

		List<AddressStruct> addresses = new List<AddressStruct>();

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				string appid = "j5MxXOPV34H6MDg6exOU6VOf8QBqSI2PPeSS3spPZK_9Qq2fx58rAYGg6Tkh51xHrw--";
				string street = tbAddress.Text;
				string zip = tbZip.Text;
				string city = tbCity.Text;
				string state = tbState.Text;
				string url = string.Format("http://local.yahooapis.com/MapsService/V1/geocode?appid={0}&street={1}&zip={2}&city={3}&state={4}", appid, street, zip, city, state);
				lbAddresses.Items.Clear();
				addresses.Clear();
				// Create the web request  
				HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

				// Get response  
				using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
				{
					// Get the response stream  
					StreamReader reader = new StreamReader(response.GetResponseStream());

					XmlDocument doc = new XmlDocument();
					doc.Load(reader);
					foreach (XmlElement node in doc.GetElementsByTagName("Result"))
					{
						AddressStruct address = new AddressStruct();

						address.Latitude = node.GetElementsByTagName("Latitude")[0].InnerText;
						address.Longitude = node.GetElementsByTagName("Longitude")[0].InnerText;
						address.City = node.GetElementsByTagName("City")[0].InnerText;
						address.State = node.GetElementsByTagName("State")[0].InnerText;
						address.Street = node.GetElementsByTagName("Address")[0].InnerText;
						addresses.Add(address);
						lbAddresses.Items.Add(address.Street + " " + address.City + ", " + address.State);
					}
				}				
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			Close();
		}

		private void lbAddresses_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lbAddresses.SelectedIndex >= 0)
				populateAddresses(lbAddresses.SelectedIndex);
		}

		private void populateAddresses(int index)
		{
			tbAddress.Text = addresses[index].Street;
			tbCity.Text = addresses[index].City;
			tbZip.Text = addresses[index].Zip;
			lblLat.Text = addresses[index].Latitude;
			lblLong.Text = addresses[index].Longitude;
			Latitude = addresses[index].Latitude;
			Longitude = addresses[index].Longitude;
		}

		private void btnSelect_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
			Close();
		}
	}
}
